name: Build and Release W&B SDK

on:
  workflow_dispatch:
    inputs:
      use_pre:
        type: boolean
        description: "pre-release"
        required: false
        default: false

jobs:
  build:
    name: Build wheels
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.9"
      - name: Install dependencies
        run: pip install -U -r requirements_build.txt
      - name: Build source and wheels
        run: |
          python -m build -n
          ls -l dist
      - uses: actions/upload-artifact@v3
        with:
          name: wandb-sdk-distributions
          path: ./dist

  test-pypi-publish:
    name: Publish to TestPyPI
    needs: build
    continue-on-error: true
    runs-on: ubuntu-latest
    environment:
      name: release
      url: https://test.pypi.org/p/wandb
    permissions:
      id-token: write  # trusted publishing
    steps:
      - name: Download distribution
        uses: actions/download-artifact@v3
        with:
          name: wandb-sdk-distributions
          path: dist/
      - name: List distribution
        run: ls dist/
      - name: Publish distribution to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          repository-url: https://test.pypi.org/legacy/

  verify-test-pypi:
    name: Verify TestPyPI upload
    needs: test-pypi-publish
    runs-on: ubuntu-latest
    steps:
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.9"
      - name: Install wandb from TestPyPI
        run: |
          sleep 5
          python -m pip install --upgrade pip
          if [ "${{ inputs.use_pre }}" == "true" ]; then
            python -m pip install --extra-index-url https://test.pypi.org/simple/ --pre wandb
          else
            python -m pip install --extra-index-url https://test.pypi.org/simple/ wandb
          fi
      - name: Smoke-test wandb TestPyPI install
        run: |
          python -c "import wandb; run = wandb.init(settings={'mode': 'offline'}); run.finish()"

  pypi-publish:
    name: Publish to PyPI
    needs: test-pypi-publish
    runs-on: ubuntu-latest
    environment:
      name: release
      url: https://pypi.org/p/wandb
    steps:
      - name: Download distribution
        uses: actions/download-artifact@v3
        with:
          name: wandb-sdk-distributions
          path: dist/
      - name: List distribution
        run: ls dist/
      - name: Publish distribution to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_TOKEN }}

  slack:
    name: Post to Slack
    needs: pypi-publish
    runs-on: ubuntu-latest
    steps:
      - name: Install from PyPI and get version
        id: get_version
        run: |
          sleep 60
          python -m pip install --upgrade pip
          if [ "${{ inputs.use_pre }}" == "true" ]; then
            python -m pip install --pre wandb
          else
            python -m pip install wandb
          fi
          echo "WANDB_VERSION=$(python -c 'import wandb; print(wandb.__version__)')" >> "$GITHUB_ENV"
      - name: Post to Slack
        id: slack
        uses: slackapi/slack-github-action@v1.24.0
        with:
          # Slack channel id, channel name, or user id to post message.
          # See also: https://api.slack.com/methods/chat.postMessage#channels
          # You can pass in multiple channels to post to by providing a comma-delimited list of channel IDs.
          channel-id: ${{ secrets.SLACK_SDK_RELEASE_CHANNEL_ID }}
          # For posting a simple plain text message
          slack-message: "W&B SDK ${{ env.WANDB_VERSION }} released :tada:: https://pypi.org/project/wandb/${{ env.WANDB_VERSION }}/"
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
